/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaServidor.java
 *
 * Created on 19/06/2011, 15:56:38
 */

package worms;

import network.*;
import network.messages.Mensagem;

/**
 *
 * @author Victor
 */
public class TelaServidor extends javax.swing.JFrame implements NetworkListener
{
	private Servidor servidor;
	private int porta;

    public TelaServidor(int porta)
	{
        initComponents();
		this.porta = porta;

		IniciarServidor();
    }

	/**
	 * Inicia o servidor
	 * @owner Victor
	 */
	private void IniciarServidor()
	{
		servidor = new Servidor();
		servidor.AddListener(this);
		servidor.Open(porta);
	}

	/**
	 * Fecha o servidor e a janela
	 * @owner Victor
	 */
	private void Close()
	{
		servidor.Close();
		this.setVisible(false);
		dispose();
	}

	/**
	 * Disparado quando uma mensagem chega
	 * @owner Victor
	 */
	public void OnMensagemChegou(Mensagem mensagem)
	{
		
	}

	/**
	 * Disparado quando uma mensagem de log chega
	 * @owner Victor
	 */
	public void OnMensagemLog(String mensagem)
	{
		txtLogServidor.append(mensagem + "\n");
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtLogServidor = new javax.swing.JTextArea();
        btnFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        txtLogServidor.setColumns(20);
        txtLogServidor.setRows(5);
        jScrollPane1.setViewportView(txtLogServidor);

        btnFechar.setText("Fechar");
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE)
                    .addComponent(btnFechar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnFechar))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	/**
	 * Disparado quando o botão de fechar é clicado
	 * @owner Victor
	 */
	private void btnFecharActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnFecharActionPerformed
	{//GEN-HEADEREND:event_btnFecharActionPerformed
		Close();
	}//GEN-LAST:event_btnFecharActionPerformed

	/**
	 * Disparado quando o botão de fechar a janela é clicado
	 * @owner Victor
	 */
	private void formWindowClosed(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosed
	{//GEN-HEADEREND:event_formWindowClosed
		Close();
	}//GEN-LAST:event_formWindowClosed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFechar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtLogServidor;
    // End of variables declaration//GEN-END:variables

}
